package b3.widget.home.town
{
	import b3.model.*;
	import b3.net.*;
	import b3.scene.home.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.text.*;
	import flash.utils.*;

	public class VitalityPane extends BasicWidget
	{
		private var vitalityBar:MaskProgressBar;
		private var clockText:TextField;
		private var vitalityText:TextField;
		private var popMsg:Sprite;
		private var timer:Timer;
		
		public function VitalityPane()
		{
			super();
			//背景
			var VitalityPaneBgr:Class = ResourceHelper.getInstance().getClassByName("VitalityPaneBgr");
			addChild(new VitalityPaneBgr());
			//体力条
			var VitalityBar:Class = ResourceHelper.getInstance().getClassByName("VitalityBar");
			vitalityBar = new MaskProgressBar(VitalityBar);
			vitalityBar.x = 12.9;
			vitalityBar.y = 3.9;
			addChild(vitalityBar);
			vitalityText = new TextField();
			vitalityText.selectable = false;
        	vitalityText.autoSize = TextFieldAutoSize.CENTER;
			vitalityText.defaultTextFormat = new TextFormat(BFont.fontName, 13, 0x000000);
			vitalityText.x = 50;
			vitalityText.y = -2;
			var pm:PlayerManager = PlayerManager.getInstance();
			vitalityText.text = pm.me.vitality.toString();
			addChild(vitalityText);
			clockText = new TextField();
			clockText.selectable = false;
        	clockText.autoSize = TextFieldAutoSize.LEFT;
			clockText.defaultTextFormat = new TextFormat(BFont.fontName, 12, 0x000000);
			clockText.x = 35;
			clockText.y = -20;
			addChild(clockText);
			pm.addEventListener(ModelEvent.SWITCH_PLAYER, onVisitFriend);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			this.mouseChildren = false;
		}
		
		private function onVisitFriend(event:ModelEvent):void
		{
			var vitalityIncrease:int = event.data['vitalityIncrease'];
			if (vitalityIncrease > 0)
			{
				playAddMovie(new Point(BFont.stageWidth / 2, BFont.stageHeight / 3), vitalityIncrease);
				var popMessage:PopMessage = new PopMessage("拜访好友，体力 +" + vitalityIncrease, 0x00AA00);
				WidgetManager.getInstance().popOut([popMessage]);
			}
		}
		
		private function onAddedToStage(event:Event):void
		{
			timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER, onTick);
			timer.start();
		}
		
		private function onRemovedFromStage(event:Event):void
		{
			if (timer != null)
			{
				timer.removeEventListener(TimerEvent.TIMER, onTick);
				timer.stop();
			}
			timer = null;
		}
		
		private function onTick(event:Event):void
		{
			var me:Player = PlayerManager.getInstance().me;
			vitalityText.text = " " + BFont.formatNumWithComa(me.vitality) + " ";
			vitalityBar.percentage = me.vitality / me.vitalityMax;
			if (me.vitality < me.vitalityMax)
			{
				var remainSecs:int = 120 - (ServerRequest.currentSeconds() - me.vitalityZeroTime) % 120;
				var remainMins:int = remainSecs / 60;
				remainSecs = remainSecs % 60;
				clockText.text = "0" + remainMins + ":" + (remainSecs < 10 ? "0" + remainSecs : remainSecs)
			}
			else
			{
				clockText.text = "";
			}
		}
			
		private function onMouseOver(event:MouseEvent):void
		{
			var container:Sprite = new Sprite();		
			//图标
			var GoldCoinIcon:Class = ResourceHelper.getInstance().getClassByName("VitalityIcon");
			var goldCoinIcon:Sprite = new GoldCoinIcon();
			goldCoinIcon.height = 60;
			goldCoinIcon.scaleX = goldCoinIcon.scaleY;
			goldCoinIcon.x = 10;
			goldCoinIcon.y = 0;
			container.addChild(goldCoinIcon);
			//金币
			var me:Player = PlayerManager.getInstance().me;
			var goldCoinText:TextField = new TextField();
			goldCoinText.selectable = false;
			goldCoinText.autoSize = TextFieldAutoSize.LEFT;
			goldCoinText.defaultTextFormat = new TextFormat("微软雅黑", 25, 0xFFFFFF, true);
			goldCoinText.filters = [new GlowFilter(0x00AA00, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			goldCoinText.text = "体力，通过拜访好友等获得";
			goldCoinText.x = 65;
			goldCoinText.y = 12;
			container.addChild(goldCoinText);
			HomeScene.getInstance().centerInfoPane.showInfo(container);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			HomeScene.getInstance().centerInfoPane.clear();
		}

		//播放体力增加动画
		public function playAddMovie(startPos:Point, num:int):void
		{
			var targetPos:Point = this.localToGlobal(new Point(50, 0));
			HomeScene.getInstance().playCoinIncreaseMovie("VitalityIcon", num, 0x00AA00, startPos, targetPos);
		}
	}
}